<?php
namespace Admin\Controller;
use Util\Upload;
use Think\Page;
class ProductController extends BaseController{
    function index(){
        if(IS_AJAX){

            $data=M('product')->field('tp_product.*,tp_category.title as ctegory')
            ->join('tp_category on tp_category.id=tp_product.cid','left')->select();
            //print_r($data);die;
           foreach($data as &$v){
               $img_arr=explode('|', $v['images']);
               $str='';
               if($img_arr){
                   foreach ($img_arr as $img){
                       $img=__ROOT__.trim($img,'.');
                       $str.="<img src='$img' height='80px' alt='load...'/>";
                   }
               }
               $v['images']=$str;
           }
            echo json_encode($data);
            //$this->ajaxReturn($data);
           /* $page=I('post.page',1);//从$_POST['page']获取当前页码
            $total=I('post.rows',10);//从$_POST['rows']获取每页行数
            $data=M('product')->page($page,$total)->select();//获取分页数据
            $count=M('product')->count();//获取总记录数

            $data=array(
                'total'=>$count,//总的记录数
                'rows'=>$data,//当前页的数据
            );
            $this->ajaxReturn($data);//返回json数据
            //die;*/
            die;
        }
        $this->display();
    }
    function add(){

        if(IS_POST){
            $upload=new Upload(1024*1024*4);
            $upload->thumbSize=0;
            $upload->waterFile='';
            
           $result=$upload->add(); 
           // dump($result);die;
            if(empty($result['error'])){
                $_POST['images']=implode('|',$result['images']);
                $_POST['ctime']=time();
                M('product')->add(I('post.'));
                $this->redirect('index');
                die;
            }else{
                $this->assign('errstr',$result['error']);
            }
        }
        $category=M('category')->order('id')->select();
        $this->assign('category',$category);
        $this->display();
    }

    public function getattr(){
        $id= I("post.id");

        $dingji = $this->getdingji($id);
        $requre_attr =M("attribute")->where(array("attr_cid"=>$dingji["id"],"is_buy"=>0))->select();
        $buy_attr =M("attribute")->where(array("attr_cid"=>$dingji["id"],"is_buy"=>1))->select();
        $this->ajaxReturn(array("requre_attr"=>$requre_attr,"buy_attr"=>$buy_attr));

    }

    function del(){
        if(IS_POST){
            $id=I('post.id',0);
            if($id){
                $result=M('product')->where(array('id'=>$id))->delete();
                if($result){
                    $data=array(
                        'code'=>1,
                        'message'=>'执行成功'
                    );

                }else{
                    $data=array(
                        'code'=>0,
                        'message'=>'删除失败'
                    );
                }
            }else{
                $data=array(
                    'code'=>0,
                    'message'=>'没有指定ID'
                );
            }
            $this->ajaxReturn($data);
        }
    }

    function getdingji($id){
        //递归方法
        $current = M("category")->where(array("id"=>$id))->find();
//        $fu = M("category")->where(array("id"=>$current["pid"]))->find();
//        $fu1 = M("category")->where(array("id"=>$fu["pid"]))->find();
       if($current["pid"]!=0) {

           $current = $this->getdingji($current["pid"]);
       }
       return $current;

    }
}